package com.small.nacos.config.server.service.dump;

import com.small.nacos.common.notify.Event;
import com.small.nacos.common.notify.listener.Subscriber;
import com.small.nacos.config.server.model.event.ConfigDumpEvent;
import com.small.nacos.config.server.service.ConfigCacheService;

public class DumpConfigHandler extends Subscriber<ConfigDumpEvent> {
    @Override
    public void onEvent(ConfigDumpEvent event) {
        configDump(event);
    }

    @Override
    public Class<? extends Event> subscribeType() {
        return ConfigDumpEvent.class;
    }

    public static boolean configDump(ConfigDumpEvent event) {
        final String dataId = event.getDataId();
        final String group = event.getGroup();
        final String namespaceId = event.getNamespaceId();
        final String content = event.getContent();
        final String type = event.getType();
        final long lastModified = event.getLastModifiedTs();
        boolean result;
        if (!event.isRemove()) {
            result = ConfigCacheService.dump(dataId, group, namespaceId, content, lastModified, type);
        } else {
            result = ConfigCacheService.remove(dataId, group, namespaceId);
        }
        return result;
    }
}
